<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\CategoryRequest;
use App\Models\Article;
use App\Models\Category;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
	/**
	 * 分类首页
	 * User: ihome
	 * @param Category $category
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
    public function index(Category $category)
    {
		//取出分类
		$categoryList = Category::withCount('article')->get()->toArray();

		$categoryList = getTree($categoryList);

        return view('admin.category.index',compact('categoryList'));
    }

	/**
	 * 添加分类展示
	 * User: ihome
	 * @param Category $category
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
    public function create(Category $category)
    {
    	//取出分类
		$categoryList = $category->all()->toArray();

		$categoryList = getTree($categoryList);

        return view('admin.category.create',compact('categoryList'));
    }

	/**
	 * 添加分类提交
	 * User: ihome
	 * @param CategoryRequest $request
	 * @param Category $category
	 * @return \Illuminate\Http\RedirectResponse
	 */
    public function store(CategoryRequest $request,Category $category)
    {
    	$category->fill($request->except('_token'));
    	$category->save();

		return redirect()->route('admin.category.index')->with('success','添加成功');
	}

	/**
	 * 修改分类
	 * User: ihome
	 * @param Category $category
	 * @param $id
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
    public function edit(Category $category,$id)
    {
		//取出分类
		$categoryList = $category->all()->toArray();
		$categoryList = getTree($categoryList);
		//取出当前分类的信息
		$cate = $category->find($id);

		return view('admin.category.edit',compact('categoryList','cate'));
    }

	/**
	 * 修改更新
	 * User: ihome
	 * @param CategoryRequest $request
	 * @param Category $categoryModel
	 * @param $id
	 * @return \Illuminate\Http\RedirectResponse
	 */
    public function update(CategoryRequest $request,Category $categoryModel,$id)
    {
    	$category = Category::findOrfail($id);
    	//获得当前分类下的子分类id
		$childIds = getChildIds('category',$id);

		$pid = $request->input('pid');

		if(in_array($pid,$childIds)){
		    return back()->with('danger','当前选择的父级分类不能是自己的子分类');
		}

		$category->update($request->except('_token'));

		return redirect()->route('admin.category.index')->with('success','修改成功');
    }

	/**
	 * 软删除
	 * User: ihome
	 * @param $id
	 * @return \Illuminate\Http\RedirectResponse
	 */
    public function destroy($id)
    {
        $category = Category::findOrfail($id);

        //删除前判断是否有子分类
		$childIds = getChildIds('category',$category->id);

		if($childIds){
		    return back()->with('danger','当前分类下有子分类，不能删除');
		}

		//如果有商品在用，不可删除
		$article = Article::query()->where('category_id',$id)->count();
		if($article){
			return back()->with('danger','该分类有文章在使用，请先删除在用的文章');
		}

        $category->delete();

		return redirect()->route('admin.category.index')->with('success','删除成功');
    }
}
